/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.course.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.course.entity.EduCourse;
import vip.xiaonuo.biz.modular.course.mapper.EduCourseMapper;
import vip.xiaonuo.biz.modular.course.param.*;
import vip.xiaonuo.biz.modular.course.service.EduCourseService;
import vip.xiaonuo.biz.modular.klass.entity.EduKlass;
import vip.xiaonuo.biz.modular.klass.service.EduKlassService;
import vip.xiaonuo.biz.modular.lecture.entity.EduLecture;
import vip.xiaonuo.biz.modular.lecture.enums.EduLectureStateEnum;
import vip.xiaonuo.biz.modular.lecture.service.EduLectureService;
import vip.xiaonuo.biz.modular.room.enums.EduRoomTypeEnum;
import vip.xiaonuo.biz.modular.tutorial.entity.EduTutorial;
import vip.xiaonuo.biz.modular.tutorial.enums.EduTutorialStateEnum;
import vip.xiaonuo.biz.modular.tutorial.service.EduTutorialService;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 课程Service接口实现类
 *
 * @author ahui
 * @date 2023/05/13 21:20
 **/
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Resource
    private EduTutorialService eduTutorialService;

    @Resource
    private EduLectureService eduLectureService;

    @Resource
    private EduKlassService eduKlassService;

    @Override
    public Page<EduCourse> page(EduCoursePageParam eduCoursePageParam) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(eduCoursePageParam.getName())) {
            queryWrapper.lambda().like(EduCourse::getName, eduCoursePageParam.getName());
        }
        if (ObjectUtil.isNotEmpty(eduCoursePageParam.getCredit())) {
            queryWrapper.lambda().eq(EduCourse::getCredit, eduCoursePageParam.getCredit());
        }
        if (ObjectUtil.isNotEmpty(eduCoursePageParam.getType())) {
            queryWrapper.lambda().like(EduCourse::getType, eduCoursePageParam.getType());
        }
        if (ObjectUtil.isAllNotEmpty(eduCoursePageParam.getSortField(), eduCoursePageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(eduCoursePageParam.getSortOrder());
            queryWrapper.orderBy(true, eduCoursePageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(eduCoursePageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(EduCourse::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(EduCourseAddParam eduCourseAddParam) {
        EduCourse eduCourse = BeanUtil.toBean(eduCourseAddParam, EduCourse.class);
        this.save(eduCourse);
    }

    @Override
    public EduCourse getByName(String name) {
        LambdaQueryWrapper<EduCourse> eduCourseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduCourseLambdaQueryWrapper.eq(EduCourse::getName, name);
        int size = this.list(eduCourseLambdaQueryWrapper).size();
        if (size != 1) {
            return null;
        }
        else {
            return this.list(eduCourseLambdaQueryWrapper).get(0);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(EduCourseEditParam eduCourseEditParam) {
        EduCourse eduCourse = this.queryEntity(eduCourseEditParam.getId());
        BeanUtil.copyProperties(eduCourseEditParam, eduCourse);
        this.updateById(eduCourse);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<EduCourseIdParam> eduCourseIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(eduCourseIdParamList, EduCourseIdParam::getId));
    }

    @Override
    public EduCourse detail(EduCourseIdParam eduCourseIdParam) {
        return this.queryEntity(eduCourseIdParam.getId());
    }

    @Override
    public EduCourse queryEntity(String id) {
        EduCourse eduCourse = this.getById(id);
        if (ObjectUtil.isEmpty(eduCourse)) {
            throw new CommonException("课程不存在，id值为：{}", id);
        }
        return eduCourse;
    }

    public List<EduCourse> selectByName(String name) {
        LambdaQueryWrapper<EduCourse> eduCourseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduCourseLambdaQueryWrapper.like(EduCourse::getName, name);
        List<EduCourse> eduCourseList = this.list(eduCourseLambdaQueryWrapper);
        if (ObjectUtil.isEmpty(eduCourseList.isEmpty())) {
            throw new CommonException("课程不存在，name值为：{}", name);
        }
        return eduCourseList;
    }

    @Override
    public void assign(EduCourseExportParam eduCourseExportParam) {
        QueryWrapper<EduCourse> eduCourseQueryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(eduCourseExportParam.getCourseIds())) {
            eduCourseQueryWrapper.lambda().in(EduCourse::getId, StrUtil.split(eduCourseExportParam.getCourseIds(), StrUtil.COMMA));
        }
        List<EduCourse> eduCourseList = this.list(eduCourseQueryWrapper);
        for (EduCourse course: eduCourseList) {
            assign(course, eduCourseExportParam);
        }
    }

    @Override
    public void assign(EduCourse course, EduCourseExportParam eduCourseExportParam) {
        EduKlass example = new EduKlass();
        example.setMajor(eduCourseExportParam.getMajor());
        example.setType(eduCourseExportParam.getStudentType());
        example.setYear(eduCourseExportParam.getGrade());
        for (EduKlass klass: eduKlassService.selectByExample(example)) {
            EduTutorial eduTutorial = new EduTutorial();
            eduTutorial.setCourseName(course.getName());
            eduTutorial.setClassHour(course.getClassHour());
            eduTutorial.setCredit(course.getCredit());
            eduTutorial.setCourseId(course.getId());
            eduTutorial.setKlassId(klass.getId());
            eduTutorial.setSize(klass.getSize());
            eduTutorial.setWeekHour(4);
            eduTutorial.setFromWeek(1);
            eduTutorial.setToWeek(course.getClassHour()/4);
            eduTutorial.setState(EduTutorialStateEnum.DOING.getValue());
            eduTutorialService.save(eduTutorial);
        }
    }

    @Override
    public void offer(EduCourseOfferParam eduCourseOfferParam) {
        QueryWrapper<EduCourse> eduCourseQueryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(eduCourseOfferParam.getCourseIds())) {
            eduCourseQueryWrapper.lambda().in(EduCourse::getId, StrUtil.split(eduCourseOfferParam.getCourseIds(), StrUtil.COMMA));
        }
        List<EduCourse> eduCourseList = this.list(eduCourseQueryWrapper);
        for (EduCourse course: eduCourseList) {
            EduLecture eduLecture = new EduLecture();
            eduLecture.setCourseId(course.getId());
            eduLecture.setKlasses("{}");
            eduLecture.setSize(60);
            eduLecture.setFromWeek(1);
            eduLecture.setToWeek(8);
            eduLecture.setState(EduLectureStateEnum.DOING.getValue());
            eduLecture.setRoomType(EduRoomTypeEnum.NONSPECIFIC.getValue());
            eduLectureService.save(eduLecture);
        }
    }
}
